package com.deam.scorpio.ui.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.deam.imsdk.bean.Conversation;
import com.deam.imsdk.bean.UserInfo;
import com.deam.imsdk.elem.BaseElem;
import com.deam.imsdk.imsdk.ConnectStateListener;
import com.deam.imsdk.imsdk.ConversationListener;
import com.deam.imsdk.imsdk.IMManager;
import com.deam.imsdk.imsdk.StartAlterListener;
import com.deam.imsdk.net.TCPClient;
import com.deam.imsdk.utils.UserUtil;
import com.deam.scorpio.R;
import com.qbkj.base_lib.adapter.QuickAdapter;
import com.qbkj.base_lib.ui.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements ConversationListener, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemChildClickListener, StartAlterListener, ConnectStateListener {
    private QuickAdapter adapter;
    private ArrayList<Conversation> datas = new ArrayList<>();
    private SwipeRefreshLayout mSwipeView;

    @Override
    protected int setContentLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        v.setText(R.id.back_btn, "");
        v.setText(R.id.menu_btn, "退出");
        mSwipeView = v.getView(R.id.swipeView);
        mSwipeView.setEnabled(true);
        mSwipeView.setOnRefreshListener(this);
        adapter = new QuickAdapter<Conversation, BaseViewHolder>(R.layout.item_conversation, datas) {
            @Override
            protected void convert(BaseViewHolder helper, Conversation item) {
                super.convert(helper, item);
                helper.addOnClickListener(R.id.conversation_layout)
                        .setText(R.id.netname, item.getNetname())
                        .setText(R.id.message, item.getElem() == null ? "" : item.getElem().getContext())
                        .setGone(R.id.start, item.getStart() == UserInfo.START_US);
            }
        };
        v.setRecyclerViewLayoutManager(R.id.recyclerView, new LinearLayoutManager(this)).setAdapter(adapter);
        adapter.setOnItemChildClickListener(this);
        IMManager.getInstance().registerConversation(this);
        IMManager.getInstance().registermStartAlter(this);
        IMManager.getInstance().registermConnectState(this);
        IMManager.getInstance().updateConversation();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        IMManager.getInstance().unRegisterConversation(this);
        IMManager.getInstance().unRegistermStartAlter(this);
        IMManager.getInstance().unRegistermConnectState(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.menu_btn:
                IMManager.getInstance().exit();
                skipActivityFinish(LoaginActivity.class);
                break;
        }
    }

    @Override
    public void updateConversation(List<Conversation> list) {
        datas.clear();
        datas.addAll(list);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void updateMessage(BaseElem elem) {
        for (int i = 0; i < datas.size(); i++) {
            Conversation conversation = datas.get(i);
            if (conversation.getUserId().equals(elem.getSenderId()) || conversation.getUserId().equals(elem.getAcceptorId())) {
                conversation.setElem(elem);
                adapter.notifyDataSetChanged();
                return;
            }
        }
    }

    @Override
    public void onRefresh() {
        mSwipeView.setRefreshing(false);
        IMManager.getInstance().updateConversation();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        mBundle = new Bundle();
        mBundle.putString("user_id", datas.get(position).getUserId());
        mBundle.putString("netname", datas.get(position).getNetname());
        skipActivity(MessageActivity.class);
    }

    @Override
    public void onBackPressed() {
        exit();
    }

    private long clickTime = 0; //记录第一次点击的时间

    private void exit() {
        if ((System.currentTimeMillis() - clickTime) > 2000) {
            toast("再按一次退出");
            clickTime = System.currentTimeMillis();
        } else {
            System.exit(0);
        }
    }

    @Override
    public void updateStart(String user_id, int start) {
        for (int i = 0; i < datas.size(); i++) {
            Conversation c = datas.get(i);
            switch (start) {
                case UserInfo.START_RE:
                case UserInfo.START_CO:

                    break;
                case UserInfo.START_US:
                    if (c.getUserId().equals(user_id)) {
                        c.setStart(start);
                        adapter.notifyDataSetChanged();
                        return;
                    }
                    break;
            }
        }
    }

    @Override
    public void onStateAlter(int state) {
        v.setVisible(R.id.not_connect_layout, true);
        switch (state) {
            case TCPClient.CONNECT_OK:
                v.setVisible(R.id.not_connect_layout, false);
                break;
            case TCPClient.CONNECT_IN:
                v.setText(R.id.connect_state, R.string.string9003);
                break;
            case TCPClient.CONNECT_NO:
                v.setText(R.id.connect_state, R.string.string9002);
                break;
        }
    }
}
